package msu.erdc.hci;

import java.util.List;

import msu.erdc.hci.customObjects.AccidentObject;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class AccidentList extends ListFragment{
	
	Button accidentButton;
	boolean firstRun;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, 
	        Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.accident_list, null);
		return view;
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //do stuff here
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{
		super.onActivityCreated(savedInstanceState);
		ListAdapter adapter = new ArrayAdapter<AccidentObject>(getActivity(), android.R.layout.simple_list_item_1, ((Global)getActivity().getApplication()).accidents);
		getListView().addHeaderView(createHeader());
		setListAdapter(adapter);
	}
	
	@Override
	public void onDestroyView()
	{
		setListAdapter(null);
		super.onDestroyView();
	}
	
	private View createHeader()
	{
		LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    	TextView tv = (TextView)inflater.inflate(android.R.layout.simple_list_item_1, getListView(), false);
    	tv.setText("+ ADD ACCIDENT");
    	tv.setGravity(Gravity.CENTER);
    	return tv;
	}
	
	@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
		int index = 0;
        if(position>0)//an actual list item clicked
        {
			index = position-1;
        }
        else//the ADD VEHICLE header was clicked
        {
//        	AccidentObject newAccident = new AccidentObject();
//        	List<AccidentObject> accidentList = ((Global)getActivity().getApplication()).accidents;
//        	accidentList.add(newAccident);
//        	index = accidentList.size()-1;
        	//Adding an accident doesn't save so now its disabled
        	Toast.makeText(getActivity(), "This feature is not implemented in this prototype", Toast.LENGTH_SHORT).show();
        	return;
        }
        //start the accident fragment
        FragmentTransaction ft = getFragmentManager().beginTransaction();
		Accident accident = new Accident();
		Bundle arguments = new Bundle();
		arguments.putInt("accidentIndex", index);
		accident.setArguments(arguments);
		ft.replace(android.R.id.content, accident, "accident");
		ft.addToBackStack(null);
		ft.commit();
    }
}
